#!/bin/bash
service_type=${SERVICE_TYPE}    #服务类型  api|crawl
instance_id=${INSTANCE_ID}		#实例ID		0,1,2...,不能重复
#algorithm_id=${ALGORITHM_ID}	#算法ID，可不设置，此时取对应服务的默认算法ID
local_ip=${LOCAL_IP}            #本地服务IP

#deploy_dir=`pwd`
deploy_dir="/opt/hms"

if [ -z ${service_type} ]; then
	echo "服务类型未指定"
	exit
fi

#工作目录根目录
work_base="/data/hms"

# 本地端口
port_step=2
port_base=14850

#设置服务名称、服务描述、执行程序
#服务类型 api|crawl
case "${service_type}" in
"api")
    description="HMS_API"
    service_name="hms_${service_type}_${instance_id}"
    execute="${deploy_dir}/bin/hms.api"
    ais_type=hms.api
    work_dir="${work_base}/hms_${service_type}_${instance_id}"
    conf_base="${deploy_dir}/scenes/hms_api"
    conf_dir="${conf_base}/config_${service_type}"
    port_service=`expr ${port_base} + ${port_step} \* ${instance_id}`
    #拷贝静态文件
    mkdir -p ${work_dir}
    static_dir="${conf_base}/static"
    cp -rf ${static_dir}  ${work_dir}/
;;
"service")
    description="HMS_SERVICE"
    service_name="hms_as_${service_type}_${instance_id}"
    execute="${deploy_dir}/bin/hms.service"
    ais_type=hms.crawl
    work_dir="${work_base}/hms_as_${service_type}_${instance_id}"
    conf_base="${deploy_dir}/scenes/hms_service"
    conf_dir="${conf_base}/config_${service_type}"
    port_service=`expr ${port_base} + ${port_step} \* ${instance_id}`
;;
*)
echo "hms install error with invalid service_type[${service_type}] "
exit
;;
esac

#清除多余空格
work_dir=$(echo -n ${work_dir} | tr '[A-Z]' '[a-z]')
# 创建程序工作目录
mkdir -p ${work_dir}

# 拷贝文件到工作目录
#清除多余空格
conf_dir=$(echo -n ${conf_dir} | tr '[A-Z]' '[a-z]')

cp -rf ${conf_dir}  ${work_dir}/config
cp -rf ${conf_base}/*.yaml ${work_dir}/


# 修改参数
sed -i "s/\${ins.aisType}/${ais_type}/g" ${work_dir}/common.yaml
sed -i "s/\${ins.localIp}/${local_ip}/g" ${work_dir}/common.yaml
sed -i "s/\${ins.servicePort}/${port_service}/g" ${work_dir}/common.yaml

# 创建service文件
hms_service="${work_dir}/${service_name}.service"
hms_service=$(echo -n ${hms_service} | tr '[A-Z]' '[a-z]')
echo -n "" > ${hms_service}
echo [Unit]																								>> ${hms_service} 
echo Description=${description}																					>> ${hms_service}  
echo																									>> ${hms_service}
echo [Service]																							>> ${hms_service} 
echo Type=simple																						>> ${hms_service} 
echo WorkingDirectory=${work_dir}																		>> ${hms_service} 
echo LimitNOFILE=65535																					>> ${hms_service}

echo ExecStart=${execute}                                                                 	>> ${hms_service} 
echo Restart=always																						>> ${hms_service} 
echo RestartSec=30																						>> ${hms_service} 
echo																									>> ${hms_service}
echo [Install]																							>> ${hms_service} 
echo WantedBy=multi-user.target																			>> ${hms_service} 

# 拷贝service文件到系统目录
cp -rf ${hms_service} /etc/systemd/system
systemctl daemon-reload
systemctl disable ${service_name}
systemctl enable ${service_name}

# 安装时启动服务
${deploy_dir}/install/start.sh
